package com.chenfan.mcn.event.subscriber;

import com.alibaba.fastjson.JSONObject;
import com.google.common.eventbus.AllowConcurrentEvents;
import com.google.common.eventbus.Subscribe;
import com.google.common.eventbus.SubscriberExceptionContext;
import com.google.common.eventbus.SubscriberExceptionHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * 总线事件订阅者
 *
 * @author wulg
 * @date 2021-11-17
 **/
public interface EventSubscriber<E> {

    /**
     * 订阅事件
     * @param event
     */
    @Subscribe
    @AllowConcurrentEvents
    void subscribe(E event);

    /**
     * 事件订阅异常处理
     * @author wulg
     * @date 2021-11-17
     **/
    @Slf4j
    class EventSubscriberExceptionHandler implements SubscriberExceptionHandler {

        @Override
        public void handleException(Throwable e, SubscriberExceptionContext context) {
            Object event = context.getEvent();
            log.error("{}-订阅异常！事件报文：{}", event.getClass().getSimpleName(), JSONObject.toJSONString(event), e);
        }
    }
}
